<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>英雄列表</title>
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/list.css">
</head>

<body>
    <div class="container">
        <h2>英雄列表</h2>
        <button>点击获取所有 LOL 英雄</button>
        <div class="hero-list">
            <div class="hero-item">
                <img src="" alt="">
                <p>英雄名称</p>
            </div>
        </div>
    </div>

    <script>
        // 1. 获取元素
        let btn = document.querySelector('button')
        let list = document.querySelector('.hero-list');
        // 2. 绑定点击事件
        btn.onclick = function(){
            let x = new XMLHttpRequest();
            x.open('get','http://localhost/getHeros');
            x.send();
            x.responseType = 'json';
            x.onreadystatechange = function(){
                if(x.readyState===4&&x.status===200){
                    // console.log(x.response);
                    let str = x.response.map(item=>`
                        <div class="hero-item">
                            <img src="${item.avatar}" alt="">
                            <p>${item.name}</p>
                        </div>
                    `).join('');
                    list.innerHTML = str;
                }
            }
        }
       
    </script>
</body>

</html>